// 设置本地存储数据
export const hsetStorage = (key, value) => {
    if (value == undefined || value == null) {
        console.warn("value值不能为undefined或者null");
        return false;
    }
    let _value = "";
    switch (typeof value) {
        case "number":
            _value = value;
            break;
        case "string":
            _value = value;
            break;
        case "object":
            _value = JSON.stringify(value);
            break;
    }
    wx.setStorageSync(key, _value)
}
// 读取本地存储数据
export const hgetStorage = (key) => {
    let _value = wx.getStorageSync(key);
    try {
        let _newValue = JSON.parse(_value);
        return _newValue;
    } catch (error) {
        return _value
    }
}
// 移除本地存储数据
export const hremoveStorage = (key) => {
    wx.removeStorageSync(key)
}
// 移除本地所有存储数据
export const hclearStorage = () => {
    wx.clearStorageSync();
}

// 设置角标
export const hsetTabBarBadge = (index, content) => {
    wx.setTabBarBadge({
        index: index,
        text: content
    })
}
// 移除角标
export const hremoveTabBarBadge = (index) => {
    wx.removeTabBarBadge({
        index: index
    })
}

// 页面跳转
export const hnavigateTo = (url, type) => {
    switch (type) {
        case "tab":
            wx.switchTab({
                url: url,
            })
            break;
        case "redirect":
            wx.redirectTo({
                url: url,
            })
            break;
        case "reLaunch":
            wx.reLaunch({
                url: url,
            })
            break;
        default:
            wx.navigateTo({
                url: url,
            })
            break;
    }
}
// 页面回退
export const hnavigateBack = (num) => {
    wx.navigateBack({
        delta: num
    })
}

// 显示loading
export const hshowLoading = (title = "加载中") => {
    return new Promise((resolve, reject) => {
        wx.showLoading({
            title: title,
            mask: true,
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                reject(res)
            },
            complete: function (res) { },
        })
    })
}
// 隐藏loading
export const hhideLoading = () => {
    wx.hideLoading();
}

// 显示 toast
export const hshowToast = (content = "操作成功", duration = 1500, icon = "none", image = "") => {
    return new Promise((resolve, reject) => {
        wx.showToast({
            title: content,
            icon: icon,
            image: image,
            duration: duration,
            mask: true,
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                reject(res)
            },
            complete: function (res) { },
        })
    })
}

// 显示 modal
export const hshowModal = (title, content) => {
    return new Promise((resolve, reject) => {
        wx.showModal({
            title: title || '',
            content: content || '',
            showCancel: true,
            confirmColor: "#f6d201",
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                reject(res)
            },
            complete: function (res) { }
        })
    })
}

/*
 *@Description: 圆角按钮 canvas
 *@param {CanvasContext} ctx canvas上下文
 *@param {number} x 圆角矩形选区的左上角 x坐标
 *@param {number} y 圆角矩形选区的左上角 y坐标
 *@param {number} w 圆角矩形选区的宽度
 *@param {number} h 圆角矩形选区的高度
 *@param {number} r 圆角的半径
*/
export const hroundRect = (ctx, bgColor = "#fff", x, y, w, h, r) => {
    // 开始绘制
    ctx.beginPath()
    // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
    // 这里是使用 fill 还是 stroke都可以，二选一即可
    ctx.setFillStyle(bgColor)
    // ctx.setStrokeStyle('transparent')
    // 左上角
    ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)

    // border-top
    ctx.moveTo(x + r, y)
    ctx.lineTo(x + w - r, y)
    ctx.lineTo(x + w, y + r)
    // 右上角
    ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)

    // border-right
    ctx.lineTo(x + w, y + h - r)
    ctx.lineTo(x + w - r, y + h)
    // 右下角
    ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)

    // border-bottom
    ctx.lineTo(x + r, y + h)
    ctx.lineTo(x, y + h - r)
    // 左下角
    ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)

    // border-left
    ctx.lineTo(x, y + r)
    ctx.lineTo(x + r, y)

    // 这里是使用 fill 还是 stroke都可以，二选一即可，但是需要与上面对应
    ctx.fill()
    // ctx.stroke()
    ctx.closePath()
    // 剪切
    // ctx.clip()
}

/* 
 * @Description: 微信列表中更多数据通用方法   0 => 没有更多数据   1  => 有更多数据   -1  =>  下拉刷新操作或者页面初始化操作
*/
export const hmoreData = (page, oldData, data, isDefaultTip = true, defaultTip = "没有更多数据") => {
    if (page == 0) {
        return {
            isHasMore: -1,
            hdata: data
        };
    }
    if (page > 0) {
        if (!data.length) {
            isDefaultTip && hshowToast(defaultTip)
            return {
                isHasMore: 0,
                hdata: oldData
            };
        }
        let _newData = oldData.concat(data);
        return {
            isHasMore: 1,
            hdata: _newData
        }
    }
}

/* 
 * @Description: 一键复制  复制到剪切板
*/
export const hsetClipboard = (str) => {
    return new Promise((resolve, reject) => {
        wx.setClipboardData({
            data: "" + str,
            success(res) {
                resolve(res)
            },
            fail(res) {
                reject(res)
            }
        })
    })
}

/* 
 * @Description: 选择微信收货地址
*/
export const hchooseAddress = () => {
    return new Promise((resolve, reject) => {
        wx.chooseAddress({
            success(res) {
                resolve(res)
            }
        })
    })
}

export const hphoneCall = (num) => {
    wx.makePhoneCall({
        phoneNumber: "" + num
    })
}